//
//  CUploadPreView.m
//  HCHK_iphone
//
//  Created by yilong zhang on 12-2-9.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "CUploadPreView.h"

@implementation CUploadPreView

@synthesize delegate;
@synthesize m_ImgView;
@synthesize m_CutView;
@synthesize m_Point;
@synthesize m_Scroll;
@synthesize m_Indicator;

@synthesize m_ReTake;
@synthesize m_TakeAgain;
@synthesize m_Used;

- (id)initWithFrame:(CGRect)frame Size:(CGSize)size Cancel:(BOOL)iscancel
{
    self = [super initWithFrame:frame];
    if (self)
    {
        m_Scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height-50)];
        m_Scroll.backgroundColor = [UIColor blackColor];
        m_Scroll.maximumZoomScale= 3.0;
        m_Scroll.minimumZoomScale= 1.0;
        m_Scroll.showsVerticalScrollIndicator = NO;
        m_Scroll.showsHorizontalScrollIndicator=NO;
        m_Scroll.delegate = self;
        [self addSubview:m_Scroll];
        [m_Scroll release];
        
        m_ImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, m_Scroll.frame.size.width, m_Scroll.frame.size.height-50)];
        m_ImgView.userInteractionEnabled = NO;
        [m_Scroll addSubview:m_ImgView];
        [m_ImgView release];
        
        m_CutView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, size.width, size.height)];
        m_CutView.userInteractionEnabled = NO;
        m_CutView.backgroundColor = [UIColor clearColor];
        m_CutView.center = CGPointMake(m_Scroll.frame.size.width/2, m_Scroll.frame.size.height/2);
        [self addSubview:m_CutView];
        [m_CutView release];
        
        //UP
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(-1, -1, self.frame.size.width+2, (m_Scroll.frame.size.height-m_CutView.frame.size.height)/2+1)];
        view.backgroundColor = [UIColor blackColor];
        view.layer.borderWidth = 1.0;
        view.layer.borderColor = [[UIColor whiteColor] CGColor];
        view.alpha = 0.6;
        [self addSubview:view];
        [view release];
        
        //DOWN
        view = [[UIView alloc] initWithFrame:CGRectMake(-1, m_CutView.frame.origin.y+m_CutView.frame.size.height, self.frame.size.width+2, (m_Scroll.frame.size.height-m_CutView.frame.size.height)/2)];
        view.backgroundColor = [UIColor blackColor];
        view.layer.borderWidth = 1.0;
        view.layer.borderColor = [[UIColor whiteColor] CGColor];
        view.alpha = 0.6;
        [self addSubview:view];
        [view release];
        
        UIImageView *back = [[UIImageView alloc] initWithFrame:CGRectMake(0, self.frame.size.height-63, self.frame.size.width, 63)];
        back.image = [UIImage imageNamed:@"tabbargb.png"];
        [self addSubview:back];
        [back release];
        
        m_ReTake = [UIButton buttonWithType:UIButtonTypeCustom];
        m_ReTake.frame = CGRectMake(10, self.frame.size.height-50, 71, 48);
        m_ReTake.tag = 0;
        [m_ReTake setBackgroundImage:[UIImage imageNamed:@"again.png"] forState:UIControlStateNormal];
        [m_ReTake addTarget:self action:@selector(OnButtonDown:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:m_ReTake];
        
        m_TakeAgain = [UIButton buttonWithType:UIButtonTypeCustom];
        m_TakeAgain.frame = CGRectMake(0, self.frame.size.height-50, 92, 50);
        m_TakeAgain.center= CGPointMake(self.frame.size.width/2, m_TakeAgain.center.y);
        m_TakeAgain.tag = 1;
        [m_TakeAgain setBackgroundImage:[UIImage imageNamed:@"camera.png"] forState:UIControlStateNormal];
        [m_TakeAgain addTarget:self action:@selector(OnButtonDown:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:m_TakeAgain];
        
        m_Used = [UIButton buttonWithType:UIButtonTypeCustom];
        m_Used.frame = CGRectMake(self.frame.size.width-81, self.frame.size.height-50, 71, 48);
        m_Used.tag = 2;
        [m_Used setBackgroundImage:[UIImage imageNamed:@"use.png"] forState:UIControlStateNormal];
        [m_Used addTarget:self action:@selector(OnButtonDown:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:m_Used];
        
        m_Indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
        m_Indicator.frame = CGRectMake(0, 0, 25, 25);
        m_Indicator.center = CGPointMake(self.frame.size.width/2, self.frame.size.height/2-20);
        m_Indicator.alpha = 0.0;
        [self addSubview:m_Indicator];
        [m_Indicator release];
    }
    return self;
}

-(void)OnButtonDown:(UIButton*)sender
{
    //[[UIApplication sharedApplication] setStatusBarHidden:NO];
    
    //retake
    if (0 == sender.tag)
    {
        [delegate DidUploaPreViewAtived:self Commond:0 Value1:nil];
    }
    //commit
    else if (2 == sender.tag)
    {
        UIImage *image = nil;
        
        //UIGraphicsBeginImageContext(m_ImgView.frame.size);
        UIGraphicsBeginImageContext(self.frame.size);
        CGContextRef context = UIGraphicsGetCurrentContext();
        [self.layer renderInContext:context];
        image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        
        CGImageRef ref = CGImageCreateWithImageInRect(image.CGImage, m_CutView.frame);
        image = [UIImage imageWithCGImage:ref];
        [delegate DidUploaPreViewAtived:self Commond:2 Value1:image];
        CGImageRelease(ref);
    }
    //take again
    else if (1 == sender.tag)
    {
        UIImage *image = nil;
        
        UIGraphicsBeginImageContext(self.frame.size);
        CGContextRef context = UIGraphicsGetCurrentContext();
        [self.layer renderInContext:context];
        image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        
        CGImageRef ref = CGImageCreateWithImageInRect(image.CGImage, m_CutView.frame);
        image = [UIImage imageWithCGImage:ref];
        [delegate DidUploaPreViewAtived:self Commond:1 Value1:image];
        CGImageRelease(ref);
    }
    else {;}
    
    m_Scroll.zoomScale = 1.0;
}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return m_ImgView;
}

-(void)SetImage:(UIImage*)image
{
    CGFloat fwidth = 0.0;
    CGFloat fheight= 0.0;
    
    fwidth = m_Scroll.frame.size.width;
    fheight = image.size.height * (m_Scroll.frame.size.width / image.size.width);

    m_Scroll.contentSize = CGSizeMake(m_Scroll.frame.size.width+2, m_Scroll.frame.size.height+44);
    
    m_ImgView.frame  = CGRectMake(0, 0, fwidth, fheight);
    m_ImgView.center = CGPointMake(m_Scroll.frame.size.width/2, m_Scroll.frame.size.height/2);
    m_ImgView.image  = image;
    
    m_Scroll.maximumZoomScale = 3.0;
    m_Scroll.minimumZoomScale = 1.1;

    [m_Scroll setContentOffset:CGPointMake(0, 0)];
    m_Indicator.alpha = 0.0;
    [m_Indicator stopAnimating];
}
/*
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    m_Point = [touch locationInView:self];
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView:self];
    
    float fx = m_Point.x - point.x;
    float fy = m_Point.y - point.y;
    
    m_CutView.center = CGPointMake(m_CutView.center.x-fx, m_CutView.center.y-fy);
    //m_CutView.center = CGPointMake(m_CutView.center.x, m_CutView.center.y-fy);
    
    m_Point = point;
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    float fx = m_ImgView.center.x - m_CutView.center.x;
    float fy = m_ImgView.center.y - m_CutView.center.y;
    
    float ff = (m_ImgView.frame.size.width-m_CutView.frame.size.width)/2;
    
    //水平方向
    if (fabs(fx) > ff)
    {
        //left
        if (fx > 0)
        {
            fx -= ff; 
        }
        //right
        else if (fx < 0)
        {
            fx += ff;
        }
        else {;}
    }
    else 
    {
        fx = 0;
    }
    
    ff = (m_ImgView.frame.size.height-m_CutView.frame.size.height)/2;
    
    //垂直方向
    if (fabs(fy) > ff)
    {
       //up
       if (fy > 0)
       {
           fy -= ff;
       }
       //down
       else if (fy < 0)
       {
           fy += ff;
       }
       else {;}
    }
    else 
    {
        fy = 0;
    }
    
    m_CutView.center = CGPointMake(m_CutView.center.x+fx, m_CutView.center.y+fy);
    //m_CutView.center = CGPointMake(m_CutView.center.x, m_CutView.center.y+fy);
}
*/
@end
